package at.fhj.swd.pse.beans;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.faces.bean.ManagedBean;
import javax.inject.Inject;
import javax.inject.Named;

import at.fhj.swd.pse.entities.Activity;
import at.fhj.swd.pse.entities.User;
import at.fhj.swd.pse.entities.UserActivity;
import at.fhj.swd.pse.helper.ActivityManager;
import at.fhj.swd.pse.helper.UserManager;

@Named
@Stateless
@ManagedBean
public class ActivityBean {

	@Inject
	private ActivityManager am;

	@Inject
	private UserManager um;

	@Named
	private String newUserActionMessage;

	@Named
	private List<Activity> activities;

	public ActivityBean() {

	}

	@PostConstruct
	public void init() {
	}

	public void newUserActivity() {
		this.am.newUserActivity(this.newUserActionMessage);
	}

	public void removeActivity(final Activity activity) {
		if (activity.getCreator().equals(this.um.getActiveUser())) {
			this.am.removeActivity(activity);
		}
	}

	public String getNewUserActionMessage() {
		return this.newUserActionMessage;
	}

	public void setNewUserActionMessage(final String newUserActionMessage) {
		this.newUserActionMessage = newUserActionMessage;
	}

	public List<Activity> getActivities() {
		return this.am.getActivities();
	}

	public void setActivities(final List<Activity> activities) {
		this.activities = activities;
	}

	public User getActiveUser() {
		return this.um.getActiveUser();
	}

	@Named
	public Boolean ActivityIsDeleteable(final Activity activity) {
		if ((activity.getCreator() == null) || (this.um.getActiveUser() == null)) {
			return false;
		}
		if ((activity instanceof UserActivity) && activity.getCreator().equals(this.um.getActiveUser())) {
			return true;
		}
		return false;
	}
}
